package com.ksd.modules.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.search.entity.SearchKeywords;
import com.ksd.modules.search.mapper.SearchKeywordsMapper;
import com.ksd.modules.search.po.SearchKeywordsSavePO;
import com.ksd.modules.search.po.SearchKeywordsSearchPO;
import com.ksd.modules.search.service.ISearchKeywordsService;
import com.ksd.modules.search.vo.SearchKeywordsVo;
import com.ksd.redis.util.RedisUtil_Back;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SearchKeywordsServiceImpl extends BaseServiceImpl<SearchKeywordsMapper, SearchKeywords> implements ISearchKeywordsService {

    @Autowired
    private RedisUtil_Back redisUtilBack;
    private static final String CACHE_KEY = "KSD:SYSTEM:MODEL:KEYWORDS:";
    /**
     * 查询列表
     * @param searchKeywordsSearchPO
     * @return
     */
    public Page<SearchKeywordsVo> findSearchKeywordss(SearchKeywordsSearchPO searchKeywordsSearchPO){
        Page<SearchKeywords> page = new Page<>(searchKeywordsSearchPO.getPageNo(),searchKeywordsSearchPO.getPageSize());
        LambdaQueryWrapper<SearchKeywords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SearchKeywords::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(SearchKeywords::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(searchKeywordsSearchPO.getStatus()), SearchKeywords::getStatus,searchKeywordsSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(searchKeywordsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like( SearchKeywords::getKeyword,searchKeywordsSearchPO.getKeyword()));
        }

        lambdaQueryWrapper.orderByAsc(SearchKeywords::getSortNum);
        Page<SearchKeywords> searchKeywordsPage = this.page(page, lambdaQueryWrapper);
        Page<SearchKeywordsVo> searchKeywordsVoPage = tranferPageBo(searchKeywordsPage, SearchKeywordsVo.class);
        return searchKeywordsVoPage;
    }

    /**
     * 回收站
     * @param searchKeywordsSearchPO
     * @return
     */
    public Page<SearchKeywordsVo> findSearchKeywordssRecyle(SearchKeywordsSearchPO searchKeywordsSearchPO){
        Page<SearchKeywords> page = new Page<>(searchKeywordsSearchPO.getPageNo(),searchKeywordsSearchPO.getPageSize());
        LambdaQueryWrapper<SearchKeywords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SearchKeywords::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(SearchKeywords::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(searchKeywordsSearchPO.getStatus()), SearchKeywords::getStatus,searchKeywordsSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(searchKeywordsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like( SearchKeywords::getKeyword,searchKeywordsSearchPO.getKeyword()));
        }

        lambdaQueryWrapper.orderByAsc(SearchKeywords::getSortNum);
        Page<SearchKeywords> searchKeywordsPage = this.page(page, lambdaQueryWrapper);
        Page<SearchKeywordsVo> searchKeywordsVoPage = tranferPageBo(searchKeywordsPage, SearchKeywordsVo.class);
        return searchKeywordsVoPage;
    }

    /**
     * 保存和更新
     * @param searchKeywordsSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public SearchKeywordsVo saveUpdateSearchKeywords(SearchKeywordsSavePO searchKeywordsSavePO){
        SearchKeywords searchKeywords = tranferBo(searchKeywordsSavePO, SearchKeywords.class);
        searchKeywords.setSystemId(SystemIdThrealLocal.get().getSystemId());
        searchKeywords.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(searchKeywords);
        if(b){
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
            SearchKeywordsVo searchKeywordsVo = tranferBo(searchKeywords, SearchKeywordsVo.class);
            return searchKeywordsVo;
        }
        return null;

    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public SearchKeywordsVo getSearchKeywordsById(Long id){
        SearchKeywords searchKeywords = this.getById(id);
        SearchKeywordsVo searchKeywordsVo = tranferBo(searchKeywords, SearchKeywordsVo.class);
        return searchKeywordsVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteSearchKeywordsById(Long id){
        boolean b =  this.updateStatus(id, SearchKeywords.Columns.IS_DELETED,1);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdSearchKeywords(Long id){
        boolean b =  this.updateStatus(id, SearchKeywords.Columns.IS_DELETED,0);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeSearchKeywordsById(Long id){
        boolean b = this.removeById(id);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesSearchKeywords(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<SearchKeywords> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversSearchKeywords(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<SearchKeywords> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesSearchKeywords(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<SearchKeywords> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<SearchKeywords> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b) {
            redisUtilBack.del(CACHE_KEY + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

}